package com.golxen.framework.common.server.cross.cmd;


import com.golxen.framework.PBPacket.PBPacket;
import com.golxen.framework.common.server.game.cmd.Command;
import com.golxen.framework.netty.session.base.ServerLinkedGroup;
import com.golxen.framework.netty.session.cross.CrossAgentLinkedSet;
import com.google.protobuf.Message;
import io.netty.channel.ChannelHandlerContext;

public abstract class AbstractCrossCommand implements Command {

	protected long serverId;
	protected int ret;

	@Override
	public Tuple execute(ChannelHandlerContext ctx, PBPacket packet) throws Exception {
		ServerLinkedGroup linkedClient = CrossAgentLinkedSet.getLinkedClient(ctx);
		serverId = linkedClient.getServerId();
		Message msg = null;
		Throwable ex = null;
		try {
			msg = parseData(packet);
			execute(packet);
			sendData();
		} catch (Throwable e) {
			ex = e;
		}
		return new Tuple(ex, msg);
	}

	public abstract Message parseData(PBPacket packet) throws Exception;

	public abstract void execute(PBPacket packet) throws Exception;

	public abstract void sendData();

}
